//
//  LoginViewController.m
//  PracticoDAW
//
//  Created by Juan Jose Alcazar Cobano on 29/04/13.
//  Copyright (c) 2013 Juan Jose Alcazar Cobano. All rights reserved.
//

#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "ListLatitudesViewController.h"
#import "ListCentroidesViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

@synthesize loginTable;

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;

- (id)init
{
    self = [super init];
    
    if (self) {
        
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(@"%@", [[self view] backgroundColor]);

    [loginTable setBackgroundView:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender
{
    NSLog(@"Login");
    
    ListLatitudesViewController *latitudes = [[ListLatitudesViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *navigationLatitudes = [[UINavigationController alloc] initWithRootViewController:latitudes];
    [[navigationLatitudes tabBarItem] setTitle:@"Latitudes"];
    [[navigationLatitudes tabBarItem] setImage:[UIImage imageNamed:@"tab1"]];
    
    ListCentroidesViewController *centroides = [[ListCentroidesViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *navigationCentroides = [[UINavigationController alloc] initWithRootViewController:centroides];
    [[navigationCentroides tabBarItem] setTitle:@"Centroides"];
    [[navigationCentroides tabBarItem] setImage:[UIImage imageNamed:@"tab2"]];
    
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    NSArray *controllers = [[NSArray alloc] initWithObjects:navigationLatitudes, navigationCentroides, nil];
    [tabBar setViewControllers:controllers];
    
    [self presentViewController:tabBar animated:YES completion:nil];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    CGRect frame = CGRectMake(20, 0, 280, 44);
    
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
        
    switch ([indexPath row]) {
        case 0:
            [textField setPlaceholder:@"Usuario o correo electronico"];
            [textField setKeyboardType:UIKeyboardTypeEmailAddress];
            [textField setReturnKeyType:UIReturnKeyNext];
            [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
            [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
            [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
            [textField setAutocorrectionType:UITextAutocorrectionTypeNo];
            [textField setEnablesReturnKeyAutomatically:YES];
            [textField setDelegate:self];
            [textField setTag:1];
            break;
            
        case 1:
            [textField setPlaceholder:@"Contraseña"];
            [textField setKeyboardType:UIKeyboardTypeDefault];
            [textField setReturnKeyType:UIReturnKeyDone];
            [textField setSecureTextEntry:YES];
            [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
            [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
            [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
            [textField setAutocorrectionType:UITextAutocorrectionTypeNo];
            [textField setEnablesReturnKeyAutomatically:YES];
            [textField setDelegate:self];
            [textField setTag:2];
            break;
            
        default:
            break;
    }
    
    [cell addSubview:textField];
    
    return cell;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // Configura el comportamiento de las teclas "NEXT" y "DONE" del teclado
    
    if ([textField tag] == 1) {
        [[[self view] viewWithTag:2] becomeFirstResponder];
    } else if ([textField tag] == 2) {
        [self login:nil];
    }
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    // Configura la animacion del movimiento de la vista al aparecer el teclado
    
    CGFloat heightFraction = 0.434783;
    
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    
    [self.view setFrame:viewFrame];
    
    [UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    // Configura la animacion del movimiento de la vista al desaparecer el teclado
    
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    
    [self.view setFrame:viewFrame];
    
    [UIView commitAnimations];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Captura los gestos para ocultar el teclado cuando haya uno
    
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [[self view] endEditing:NO];
    }
}

- (UIView *)footerView
{    
    if (!footerView) {
        [[NSBundle mainBundle] loadNibNamed:@"LoginFooterView" owner:self options:nil];
    }
    
    return footerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    [loginButton setBackgroundColor:[UIColor colorWithRed:0.28 green:0.69 blue:0.76 alpha:1.0]];
    [[loginButton layer] setCornerRadius:5.0];
    [[loginButton layer] setMasksToBounds:YES];
    
    return [self footerView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{    
    return [[self footerView] bounds].size.height;
}

@end
